﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;

namespace gpuimp.core.ColorConverter
{
    public class ColorManager
    {
        private Color foregroundColor;
        private Color backgroundColor;
        private Color mixerColor;
        private Color blendColor1;
        private Color blendColor2;

        public List<Color> recentColors;

        public ColorManager()
        {
            recentColors = new List<Color>();
            foregroundColor = Colors.Black;
            backgroundColor = Colors.White;           
        }
        public void addColor(Color cpc)
        {
            if (recentColors.Count > 23)
            {
                recentColors.RemoveAt(0);
            }
            if(recentColors.Contains(cpc)){

            }
            else{
                recentColors.Add(cpc);
            }            
        }
        public void setForeground(Color cpc)
        {
            foregroundColor = cpc;
        }
        public void setMixer(Color cpc)
        {
            mixerColor = cpc;
        }
        public void setBlendColor1(Color cpc)
        {
            blendColor1 = cpc;
        }
        public void setBlendColor2(Color cpc)
        {
            blendColor2 = cpc;
        }
        public void setBackground(Color cpc)
        {
            backgroundColor = cpc;
        }
        public Color getForeground()
        {
            return foregroundColor;
        }
        public Color getMixer()
        {
            return mixerColor;
        }
        public Color getBlendColor1()
        {
            return blendColor1;
        }
        public Color getBlendColor2()
        {
            return blendColor2;
        }
        public Color getBackground()
        {
            return backgroundColor;
        }
        public List<Color> getRecentColors()
        {
            return recentColors;
        }
        public Color convertHSLtoRGB(double r, double g, double b)
        {
            return ColorConverter.HSLToRGB(r, g, b);
        }
        public string colorToHexString(Color color)
        {
            char[] hexDigits = {
            '0', '1', '2', '3', '4', '5', '6', '7',
            '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};


            byte[] bytes = new byte[3];
            bytes[0] = color.R;
            bytes[1] = color.G;
            bytes[2] = color.B;
            char[] chars = new char[bytes.Length * 2];
            for (int i = 0; i < bytes.Length; i++)
            {
                int b = bytes[i];
                chars[i * 2] = hexDigits[b >> 4];
                chars[i * 2 + 1] = hexDigits[b & 0xF];
            }
            return new string(chars);
        }
        public List<Color> generateColors(Color c1, Color c2, int steps)
        {
            List<Color> blended = new List<Color>();

            int c1R = Convert.ToInt32(c1.R);
            int c2R = Convert.ToInt32(c2.R);

            int c1G = Convert.ToInt32(c1.G);
            int c2G = Convert.ToInt32(c2.G);

            int c1B = Convert.ToInt32(c1.B);
            int c2B = Convert.ToInt32(c2.B);

            int c1A = Convert.ToInt32(c1.A);
            int c2A = Convert.ToInt32(c2.A);

            blended.Add(c1);



            try
            {
                for (int i = 1; i <= steps + 1; i++)
                {
                    Color newColor = new Color();

                    if (c1R >= c2R)
                    {
                        newColor.R = Convert.ToByte(c1R - (i * ((c1R - c2R) / steps)));
                    }
                    else
                    {
                        newColor.R = Convert.ToByte(c1R + (i * ((c2R - c1R) / steps)));
                    }

                    if (c1G >= c2G)
                    {
                        newColor.G = Convert.ToByte(c1G - (i * ((c1G - c2G) / steps)));
                    }
                    else
                    {
                        newColor.G = Convert.ToByte(c1G + (i * ((c2G - c1G) / steps)));
                    }

                    if (c1B >= c2B)
                    {
                        newColor.B = Convert.ToByte(c1B - (i * ((c1B - c2B) / steps)));
                    }
                    else
                    {
                        newColor.B = Convert.ToByte(c1B + (i * ((c2B - c1B) / steps)));
                    }

                    if (c1A >= c2A)
                    {
                        newColor.A = Convert.ToByte(c1A - (i * ((c1A - c2A) / steps)));
                    }
                    else
                    {
                        newColor.A = Convert.ToByte(c1A + (i * ((c2A - c1A) / steps)));
                    }

                    blended.Add(newColor);

                }
                blended.Add(c2);
            }
            catch (Exception)
            {
                
                
            }
            return blended;
        }
        public List<Color> basicPalette()
        {
            List<Color> basicColors = new List<Color>();
            
           
            
            basicColors.Add(Color.FromRgb(204, 0, 0));      // red
            basicColors.Add(Color.FromRgb(255, 153, 0));    // orange
            basicColors.Add(Color.FromRgb(255, 255, 0));    // yellow
            basicColors.Add(Color.FromRgb(0, 255, 0));      // green
            basicColors.Add(Color.FromRgb(0, 255, 255));    // cyan
            basicColors.Add(Color.FromRgb(51, 102, 255));   // blue
            basicColors.Add(Color.FromRgb(255, 0, 255));    // magenta
            basicColors.Add(Color.FromRgb(102, 0, 102));    // purple
            basicColors.Add(Color.FromRgb(153, 0, 0));      // dark red
            basicColors.Add(Color.FromRgb(153, 153, 0));    // khaki
            basicColors.Add(Color.FromRgb(0, 102, 0));     // dark green
            basicColors.Add(Color.FromRgb(51, 51, 102));   // dark blue
            basicColors.Add(Color.FromRgb(102, 51, 0));    // brown
            basicColors.Add(Color.FromRgb(204, 204, 204)); // light gray
            basicColors.Add(Color.FromRgb(0, 0, 0));       // black
            basicColors.Add(Color.FromRgb(102, 204, 255)); // sky
            basicColors.Add(Color.FromRgb(255, 204, 255)); // pink
            basicColors.Add(Color.FromRgb(255, 255, 204)); // chiffon
            basicColors.Add(Color.FromRgb(255, 204, 204)); // flesh
           basicColors.Add(Color.FromRgb(153, 255, 204)); // pale green

           return basicColors;
        }

    }
}
